<?php

namespace App\Http\Resources;

use App\Models\Entity;
use App\Models\PoliceStation;
use App\Models\School;
use Illuminate\Http\Resources\Json\Resource;

class PrintResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $entity = Entity::query()->where('id', $this->entity_id)->first();
        return [
            'id' => $this->id,
            'name' => (string)$this->name,
            'sex' => (string)$this->sex,
            'twins' => (int)$this->twins,
            'is_twins' => $this->is_twins == 1 ? true :false,
            'nation' => (string)$this->nation,
            'id_code' => (string)$this->id_code,
            'prophylactic_code' => (string)$this->prophylactic_code,
            'police_name' => (string)$this->getPoliceName($this->police_station_id),
            'relation' => (string)$this->relation,
            'family_address' => (string)$entity->family_address,
            'school_name' => (string)$this->getSchoolName($this->school_id),
            'father_name' => (string)$entity->father_name,
            'father_type' => (string)$entity->father_type,
            'father_id_code' => (string)$entity->father_id_code,
            'father_works' => (string)$entity->father_works,
            'father_phone_number' => (string)$entity->father_phone_number,
            'mother_name' => (string)$entity->mother_name,
            'mother_type' => (string)$entity->mother_type,
            'mother_id_code' => (string)$entity->mother_id_code,
            'mother_works' => (string)$entity->mother_works,
            'mother_phone_number' => (string)$entity->mother_phone_number,
            'registration_numbers' => (string)$this->getRegistrationNumbers($this->school_id),
        ];
    }

    public function getPoliceName($police_station_id)
    {
        $police = PoliceStation::query()->where('id', $police_station_id)->first();
        $policeName = $police->name;
        return $policeName;
    }

    public function getSchoolName($school_id)
    {
        $school = School::query()->where('school_code', $school_id)->first();
        $schoolName = $school->school_name;
        return $schoolName;
    }

    public function getRegistrationNumbers($school_id)
    {
        $school = School::query()->where('school_code', $school_id)->first();
        $registration_numbers = $school->registration_numbers;
        return $registration_numbers;
    }
}